package it.cqupt.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import it.cqupt.dao.system.DeptDao;
import it.cqupt.dao.system.ModuleDao;
import it.cqupt.domain.system.Dept;
import it.cqupt.domain.system.Module;
import it.cqupt.factory.MapperFactory;
import it.cqupt.service.system.ModuleService;
import it.cqupt.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;
import java.util.UUID;

public class ModuleServiceImpl implements ModuleService {
    SqlSession sqlSession = null;
    @Override
    public void save(Module module) {
        try {
            sqlSession = MapperFactory.getSqlSession();
            ModuleDao mapper = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            String uuid = UUID.randomUUID().toString();
            module.setId(uuid);
            mapper.save(module);
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public void delete(Module module) {
        try {
            sqlSession = MapperFactory.getSqlSession();
            ModuleDao mapper = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            mapper.delete(module);
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public void update(Module module) {
        try {
            sqlSession = MapperFactory.getSqlSession();
            ModuleDao mapper = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            mapper.update(module);
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public Module findById(String id) {
        try {
            sqlSession = MapperFactory.getSqlSession();
            ModuleDao mapper = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            return mapper.findById(id);
        }catch (Exception e){
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Module> findAll() {
        try {
            sqlSession = MapperFactory.getSqlSession();
            ModuleDao mapper = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            return mapper.findAll();
        }catch (Exception e){
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public PageInfo findAll(int page, int size) {
        try {
            sqlSession = MapperFactory.getSqlSession();
            ModuleDao mapper = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            PageHelper.startPage(page,size);
            List<Module> list = mapper.findAll();
            PageInfo pageInfo = new PageInfo(list);
            return pageInfo;
        }catch (Exception e){
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Map> findAuthorDataByRoleId(String roleId) {
        try {
            sqlSession = MapperFactory.getSqlSession();
            ModuleDao mapper = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            return mapper.findAuthorDataByRoleId(roleId);
        }catch (Exception e){
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
